﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

public partial class frmForgottenPassword : System.Web.UI.Page
{

    #region EVENT MODULES

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnResend_Click(object sender, EventArgs e)
    {
        //### Validate email
        if (txtEmailAddress.Text == "")
        {
            txtEmailAddress.Attributes.Add("style", "border:1px solid #911f22");
            lblMessage.Text = "<b class=\"errorMsg\">Please enter your email address.</b>";
        }
        else
        {
            //### Check if email address exists
            if (clsCommonFunctions.DoesRecordExist("tblUsers", "strEmailAddress = '" + txtEmailAddress.Text + "' AND bIsActive = 1") == true)
            {
                //### Change password & Send mail
                string strRandomPassword = "";
                string strPassword = "";

                strRandomPassword = clsCommonFunctions.strCreateRandomPassword(8);
                strPassword = strRandomPassword;

                //### Hash random password
                strRandomPassword = clsCommonFunctions.GetMd5Sum(strRandomPassword);

                DataConnection.GetDataObject().ExecuteScalar("spForgottenPassword '" + txtEmailAddress.Text + "', '" + strRandomPassword + "'");

                sendMail(strPassword);

                //### Redirect
                Response.Redirect("frmThankYou.aspx?val=passwordreset");

            }
            else
            {
                //### If no email address found
                lblMessage.Text = "<b class=\"errorMsg\">The email you have entered is not a registered <br />email address.</b><br /><br /><br />";
            }
        }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtEmailAddress.Text = "";
    }

    #endregion

    #region MAIL METHODS

    private void sendMail(string strRandomPassword)
    {
        //### Create an instance of the MailMessage class 
        MailMessage myMail = new MailMessage();

        //### Set the subject
        myMail.Subject = "Ebeds Forgotten Password";

        myMail.From = new MailAddress("noreply@ebeds.co.za");

        myMail.To.Add(txtEmailAddress.Text);

        myMail.IsBodyHtml = true;

        myMail.Body += "<html><head></head><title></title><body>";

        myMail.Body += "<table cellpadding=\"2\" cellspacing=\"2\" style=\"font-family:Arial; font-size:12; width:520px; border:1px solid #333333; background:#fff;\">";
        myMail.Body += "<tr><td colspan=\"2\" style=\"background:#681b13; color:#fff; font-weight:bold\">" + myMail.Subject + "</td></tr>";
        myMail.Body += "<tr><td colspan=\"2\">Dear User</td></tr>";
        myMail.Body += "<tr><td colspan=\"2\">Your temporary password is shown below, please log into your account and change your password to something more suitable and easier to remember:</td></tr>";
        myMail.Body += "<tr><td style=\"width:120px\"><b>Password:</b></td><td style=\"text-align:left; width:400px\">" + strRandomPassword + "</td></tr>";
        myMail.Body += "<tr><td colspan=\"2\"><b>Regards</b></td></tr>";
        myMail.Body += "<tr><td colspan=\"2\"><a href=\"http://www.ebeds.co.za\" style=\"text-decoration:none; color:#3c5e8b\">ebeds</a></td></tr>";

        myMail.Body += "</table>";

        myMail.Body += "</body></html>";

        //### Password protected
        SmtpClient emailClient = new SmtpClient("smtp.ebeds.co.za");
        emailClient.Credentials = new System.Net.NetworkCredential("noreply@ebeds.co.za", "3beds001");

        //### Now, to send the message, use the Send method of the SmtpMail class 
        emailClient.Send(myMail);
    }

    #endregion
}
